package com.chisheng.caa.util;

import com.chisheng.caa.config.CaaProperties;
import com.chisheng.caa.service.CacheUtil;
import com.chisheng.caa.service.ExpiryMap;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Map 缓存访问工具类
 */
@Component
public class ExpiryMapCacheUtil implements CacheUtil {

    @Resource
    private CaaProperties properties;

    @Resource
    private ExpiryMap<String, Object> expiryCache;

    @Override
    @SuppressWarnings("unchecked")
    public <T> T get(String accessKey) {
        return (T) expiryCache.get(accessKey);
    }

    @Override
    public String put(Object obj) {
        return put(obj, properties.getMaxTimeToLive());
    }

    @Override
    public void put(String key, Object value, long expiryTime) {
        if (expiryTime == -1) {
            throw new UnsupportedOperationException();
        }
        expiryCache.put(key, value, expiryTime);
    }

    @Override
    public void remove(String key) {
        expiryCache.remove(key);
    }

}
